--------------------------------------------------------------------------------
-- G600 統合スクリプト（LGS専用） - G15トリガー版
-- 4スキル優先順位・CT・キャストタイム付き自動ローテーション（制限＆キャスト内保持対応）
--------------------------------------------------------------------------------

-- 【重要】マウスのボタンイベントを確実に拾うための有効化
EnablePrimaryMouseButtonEvents(true)

-- ==========================================
-- G15 スキル回し用設定（ミリ秒）
-- ==========================================
-- クールダウン (CT)
local CT_key1 = 4020
local CT_key2 = 12020
local CT_key3 = 7520
local CT_key4 = 6520
local CT_key5 = 1020

-- キャストタイム（硬直・切り替わり制限時間）
local Cast_key1 = 1100
local Cast_key2 = 450
local Cast_key3 = 517
local Cast_key4 = 1100
local Cast_key5 = 1100

-- 状態管理用変数
local CD_key1 = 0
local CD_key2 = 0
local CD_key3 = 0
local CD_key4 = 0
local CD_key5 = 0

local CastEndTick = 0               -- キャスト硬直終了予定時刻

-- 直前に使用したスキルを記録する変数
local last_skill = ""


--------------------------------------------------------------------------------
-- イベントハンドラ
--------------------------------------------------------------------------------
function OnEvent(event, arg)
    local currentTick = GetRunningTime()

    -- ==================================================================
    -- 【マウスボタンが押されたイベント】
    -- ==================================================================
    if (event == "MOUSE_BUTTON_PRESSED") then
        
        -- ==================================================================
        -- 【ボタン 15 の処理】：4スキル自動ローテーション
        -- ==================================================================
        if (arg == 15) then
            -- キャストタイム中の処理（連打時の保持・入力受付）
            if (currentTick < CastEndTick) then
                if (last_skill ~= "") then
                    SendSkillTwice(last_skill)
                    -- キャストタイムを更新（連打時に硬直を維持）
                    local current_cast = 477
                    if last_skill == "1" then current_cast = Cast_key1
                    elseif last_skill == "2" then current_cast = Cast_key2
                    elseif last_skill == "3" then current_cast = Cast_key3
                    elseif last_skill == "4" then current_cast = Cast_key4
                    elseif last_skill == "5" then current_cast = Cast_key5
                    end
                    CastEndTick = currentTick + current_cast
                end
                return
            end

            -- 3. key3 の判定
            if (currentTick >= CD_key3) then
                SendSkillTwice("3")
                CastEndTick = currentTick + Cast_key3
                CD_key3 = currentTick + CT_key3
                last_skill = "3"
                return
            end

            -- 4. key4 の判定
            if (currentTick >= CD_key4) then
                SendSkillTwice("4")
                CastEndTick = currentTick + Cast_key4
                CD_key4 = currentTick + CT_key4
                last_skill = "4"
                return
            end

            -- 2. key2 の判定 (※ key1 または key3 の直後は使用不可)
            if (currentTick >= CD_key2 and last_skill ~= "1" and last_skill ~= "3") then
                SendSkillTwice("2")
                CastEndTick = currentTick + Cast_key2
                CD_key2 = currentTick + CT_key2
                key2_cancel_deadline = CastEndTick
                last_skill = "2"
                return
            end

            -- 1. key1 の判定 (※ key4 の直後は使用不可)
            if (currentTick >= CD_key1 and last_skill ~= "4") then
                SendSkillTwice("1")
                CastEndTick = currentTick + Cast_key1
                CD_key1 = currentTick + CT_key1
                last_skill = "1"
                return
            end

            -- フォールバック: key5
            SendSkillTwice("5")
            CastEndTick = currentTick + Cast_key5
            CD_key5 = currentTick + CT_key5
            last_skill = "5"
            return
        end

    end
end

--------------------------------------------------------------------------------
-- 補助関数
--------------------------------------------------------------------------------
function PressAndReleaseKey(key)
    PressKey(key)
    Sleep(36)
    ReleaseKey(key)
end

function SendSkillTwice(key)
    PressKey(key)
    Sleep(36)
    ReleaseKey(key)
end